package com.cxs.dao.impl;

import com.cxs.annotation.TableId;
import com.cxs.dao.BaseDao;
import com.cxs.exceptions.PrimaryMissException;
import com.cxs.handler.impl.ResultBeanAsBeanHandler;
import com.cxs.handler.impl.ResultBeanAsListHandler;
import com.cxs.template.JdbcTemplate;
import com.cxs.utils.UnderlineToHumpCaseUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;

/**
 * @Project: jdbc
 * @Author: cxs2014501@163.com
 * @Create: 2022/9/1 10:58
 * @Description:
 **/
public class BaseDaoImpl<T> implements BaseDao<T> {

    private static Logger logger = LogManager.getLogger(BaseDaoImpl.class);

    private Class<T> clazz;

    public BaseDaoImpl(Class<T> clazz){
        this.clazz = clazz;
    }

    @Override
    public int insert(T t) {
        int result = 0;
        try {
            String tableName = getTableName();
            String insertSql = getInsertSql(t, tableName, Boolean.FALSE);
            logger.info("sql:===> {}", insertSql);
            Object[] values = getTValue(t, Boolean.FALSE);
            logger.info("params:{}", Arrays.asList(values));
            result = JdbcTemplate.executeDMLSql(insertSql, values);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<T> selectAll() {
        String sql = "select * from " + getTableName();
        logger.info("sql:===> {}", sql);
        return JdbcTemplate.executeDQLSql(sql, new ResultBeanAsListHandler<T>(clazz));
    }

    @Override
    public int insertSelective(T t) {
        int result = 0;
        try {
            String tableName = getTableName();
            String insertSql = getInsertSql(t, tableName, Boolean.TRUE);
            Object[] values = getTValue(t, Boolean.TRUE);
            logger.info("sql:===> {}", insertSql);
            logger.info("params:{}", Arrays.asList(values));
            result = JdbcTemplate.executeDMLSql(insertSql, values);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int update(T t) {
        int result = 0;
        try {
            String tableName = getTableName();
            String updateSql = getUpdateSql(t, tableName, Boolean.FALSE);
            Object[] values = getUpdateTValue(t, Boolean.FALSE);
            logger.info("sql:===> {}", updateSql);
            logger.info("params:{}", Arrays.asList(values));
            result = JdbcTemplate.executeDMLSql(updateSql, values);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int updateSelective(T t) {
        int result = 0;
        try {
            String tableName = getTableName();
            String updateSql = getUpdateSql(t, tableName, Boolean.TRUE);
            logger.info("sql:===> {}", updateSql);
            Object[] values = getUpdateTValue(t, Boolean.TRUE);
            logger.info("params:{}", Arrays.asList(values));
            result = JdbcTemplate.executeDMLSql(updateSql, values);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int deleteById(Object id) {
        String tableName = getTableName();
        String sql = "delete from " + tableName + " where " + getIdColumn() + " = ?";
        logger.info("sql:===> {}", sql);
        logger.info("params:{}", Arrays.asList(id));
        return JdbcTemplate.executeDMLSql(sql, id);
    }

    @Override
    public T selectById(Object id) {
        String sql = "select * from " + getTableName() + " where " + getIdColumn() + " = ?";
        logger.info("sql:===> {}", sql);
        logger.info("params:{}", Arrays.asList(id));
        return JdbcTemplate.executeDQLSql(sql, new ResultBeanAsBeanHandler<>(clazz), id);
    }

    private String getTableName() {
        return UnderlineToHumpCaseUtil.humpCaseToUnderline(clazz.getSimpleName());
    }


    private Object[] getUpdateTValue(T t, Boolean flag) throws IllegalAccessException {
        List<Object> list = new ArrayList<>();
        Field[] fields = clazz.getDeclaredFields();
        String primaryKey = null;
        Object primaryValue = null;
        if (null != fields && fields.length > 0) {
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.isAnnotationPresent(TableId.class)) {
                    primaryKey = UnderlineToHumpCaseUtil.humpCaseToUnderline(field.getName());
                    primaryValue = field.get(t);
                }
            }
            if (primaryKey == null || primaryValue == null) {
                throw new PrimaryMissException("table \"" + getTableName() + "\" miss annocation @TableId");
            }
            if (flag) {
                for (Field field : fields) {
                    field.setAccessible(true);
                    Object o = field.get(t);
                    if (null != o && !UnderlineToHumpCaseUtil.humpCaseToUnderline(field.getName()).equals(primaryKey)){
                        list.add(o);
                    }
                }
            } else {
                for (Field field : fields) {
                    field.setAccessible(true);
                    Object o = field.get(t);
                    if (!UnderlineToHumpCaseUtil.humpCaseToUnderline(field.getName()).equals(primaryKey)){
                        list.add(o);
                    }
                }
            }
            list.add(primaryValue);
        }
        return list.toArray(new Object[0]);
    }

    /**
     * 获得修改的SQL
     * @param t
     * @param tableName
     * @param flag
     * @return
     */
    private String getUpdateSql(T t, String tableName, Boolean flag) throws IllegalAccessException {
        StringBuilder sqlBuild = new StringBuilder("update ");
        sqlBuild.append(tableName);
        sqlBuild.append(" set ");
        String primaryKey = null;
        Object primaryValue = null;
        Field[] fields = clazz.getDeclaredFields();
        if (null != fields && fields.length > 0) {
            StringJoiner joiner = new StringJoiner(",");
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.isAnnotationPresent(TableId.class)) {
                    primaryKey = UnderlineToHumpCaseUtil.humpCaseToUnderline(field.getName());
                    primaryValue = field.get(t);
                }
            }
            if (primaryKey == null || primaryValue == null) {
                throw new PrimaryMissException("table \"" + tableName + "\" miss annocation @TableId");
            }
            if (flag) {
                for (Field field : fields) {
                    field.setAccessible(true);
                    String name = field.getName();
                    Object value = field.get(t);
                    if (null != value && !UnderlineToHumpCaseUtil.underlineToHumpCase(name).equals(primaryKey)) {
                        joiner.add(UnderlineToHumpCaseUtil.humpCaseToUnderline(name) + "=?");
                    }
                }
                sqlBuild.append(joiner.toString());
            } else {
                for (Field field : fields) {
                    field.setAccessible(true);
                    String name = field.getName();
                    if (!UnderlineToHumpCaseUtil.underlineToHumpCase(name).equals(primaryKey)) {
                        joiner.add(UnderlineToHumpCaseUtil.humpCaseToUnderline(name) + "=?");
                    }
                }
                sqlBuild.append(joiner.toString());
            }
            sqlBuild.append(" where ");
            sqlBuild.append(primaryKey);
            sqlBuild.append(" = ?");
        }
        return sqlBuild.toString();
    }

    private Object[] getTValue(T t, Boolean flag) throws IllegalAccessException {
        List<Object> list = new ArrayList<>();
        Field[] fields = clazz.getDeclaredFields();
        if (null != fields && fields.length > 0) {
            if (flag) {
                for (Field field : fields) {
                    field.setAccessible(true);
                    Object o = field.get(t);
                    if (null != o){
                        list.add(o);
                    }
                }
            } else {
                for (Field field : fields) {
                    field.setAccessible(true);
                    Object o = field.get(t);
                    list.add(o);
                }
            }
        }
        return list.toArray(new Object[0]);
    }

    private String getInsertSql(T t, String tableName, Boolean flag) throws IllegalAccessException {
        StringBuilder sqlBuild = new StringBuilder("insert into ");
        StringJoiner params = new StringJoiner(",");
        sqlBuild.append(tableName);
        sqlBuild.append("(");
        Field[] fields = clazz.getDeclaredFields();
        if (null != fields && fields.length > 0) {
            StringJoiner joiner = new StringJoiner(",");
            if (flag) {
                for (Field field : fields) {
                    field.setAccessible(true);
                    String name = field.getName();
                    Object value = field.get(t);
                    if (null != value) {
                        joiner.add(UnderlineToHumpCaseUtil.humpCaseToUnderline(name));
                        params.add("?");
                    }
                }
                sqlBuild.append(joiner.toString());
            } else {
                for (Field field : fields) {
                    String name = field.getName();
                    joiner.add(UnderlineToHumpCaseUtil.humpCaseToUnderline(name));
                    params.add("?");
                }
                sqlBuild.append(joiner.toString());
            }
            sqlBuild.append(") ");
            sqlBuild.append("values(");
            sqlBuild.append(params.toString());
            sqlBuild.append(")");
        }
        return sqlBuild.toString();
    }

    private String getIdColumn(){
        Field[] fields = clazz.getDeclaredFields();
        if (null != fields && fields.length > 0) {
            for (Field field : fields) {
                if (field.isAnnotationPresent(TableId.class)) {
                    return UnderlineToHumpCaseUtil.humpCaseToUnderline(field.getName());
                }
            }
        }
        throw new PrimaryMissException("table \"" + getTableName() + "\" miss annocation @TableId");
    }
}
